#This to obtain:
#Figure_A15a.jpg
#Figure_A15b.jpg

rm(list = ls())
library("sf")
library("ggplot2")
library("dplyr")
library("ggrepel")
library("raster")
library("RColorBrewer")
library("lemon")
library("sp")
library("maptools")
library("ggsn")
library("gstat")
library("gtable")
library("lemon")

#setwd("C:/Users/bogdanp/")
setwd("/Users/bgpopescu/")

####################
#Reading Shapefiles#
####################

HRV_adm0 <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                layer="HRV_adm0_wgs")
HRV_adm0<-st_simplify(HRV_adm0, dTolerance = 500)

river <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                          layer="rivers")
river = st_transform(river, st_crs(HRV_adm0))
river<-st_simplify(river,  dTolerance = 0.005)

krajna_brd <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                  layer="krajna6_river_merged")
krajna_brd<-st_simplify(krajna_brd,  dTolerance = 0.005)


#Reading Polygons
x<-st_layers(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb")


HRV_adm0 <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                    layer="HRV_adm0_wgs")

krajna <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                  layer="krajna_line_GCS")

HRV_adm3 <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                    layer="HRV_adm3")
HRV_adm3<-st_simplify(HRV_adm3, dTolerance = 0.005)
HRV_adm3<-subset(HRV_adm3, select = c(ID_2))

HRV_adm3_data = read_excel("./Dropbox/Legacies_Project/Analysis/data/merge.xlsx", sheet=1, col_names = TRUE, skip = 0)

final_sp<-left_join(HRV_adm3, HRV_adm3_data, by = c("ID_2"="ID_2"))
final_sp_coord<-subset(final_sp, select = c(ID_2, krajna_river_distance, krajna_river_NEAR_FID, river))
final_sp_coord = st_transform(final_sp_coord, st_crs(krajna_brd))
final_sp_coord_yes<-subset(final_sp_coord, river==1)
final_sp_coord_no<-subset(final_sp_coord, river==0)

#######################
#Elevation for Croatia#
#######################

#Reading Rasters
euro_raster <- raster("./Dropbox/Legacies_Project/Analysis/data/euro_raster.tif")
HRV_adm0$y_lat<-sf::st_coordinates(st_centroid(HRV_adm0))[,1]
HRV_adm0$x_lon<-sf::st_coordinates(st_centroid(HRV_adm0))[,2]

max_lat<-max(HRV_adm0$y_lat) + 4
min_lat<-min(HRV_adm0$y_lat) - 4

max_lon<-max(HRV_adm0$x_lon) + 4
min_lon<-min(HRV_adm0$x_lon) - 4

#e <- extent(-160, 10, 30, 60)
e <- extent(min_lat, max_lat, min_lon, max_lon)
rc <- crop(euro_raster, e)	
res(rc)

#Lower resolution raster
rc.aggregate <- aggregate(rc, fact=2)
res(rc.aggregate)

#Get rid of really high mountains
rc <- reclassify(rc.aggregate, c(10000,Inf,NA))
rm(rc.aggregate)
rna <- reclassify(rc, cbind(NA, 0))
rm(rc)
#Convert rasters TO dataframes for plotting with ggplot
hdf <- rasterToPoints(rna); hdf <- data.frame(hdf)
colnames(hdf) <- c("X","Y","Hill")

#   Create vectors for colour breaks
b.hs <- seq(min(hdf$Hill),max(hdf$Hill),length.out=100)

krajna_brd$coincides<-NA
krajna_brd$coincides[krajna_brd$river==1]<-"Yes"
krajna_brd$coincides[krajna_brd$river==0]<-"No"


map_river_graph<-ggplot()+
  geom_raster(data=hdf,aes(X,Y,alpha=Hill)) +
  scale_alpha(name = "Altitude in m", guide="none")  + 
  geom_sf(data = HRV_adm0, fill = NA, colour = "black", linewidth = 0.8)+
  geom_sf(data = river, colour = "blue", linewidth = 0.2)+
  geom_sf(data = krajna_brd, aes(colour = coincides), 
          linewidth = 0.5, show.legend = T)+
  scale_color_manual(name = "Coincides River?", values=c("red","blue"))+
  coord_sf(ylim=c(42.4,46.669853), xlim=c(13.556495, 19.447201))+
  theme(legend.position="left")+
  theme_bw()+
  labs(x = "Longitude", y="Latitude")+
  theme(axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title=element_text(size=14),
        plot.title = element_text(hjust = 0.5),
        legend.position = c(1, 0),
        #Legend.position values should be between 0 and 1. c(0,0) corresponds to the "bottom left"
        #and c(1,1) corresponds to the "top right" position.
        legend.box.background = element_rect(fill='white'),
        legend.background = element_blank(),
        legend.text=element_text(size=12))+
  ggsn::scalebar(x.min = 18.5, x.max = 19.1,
           #Above you mention how long the scalebar should be
           y.min = 46.4, y.max = 46.7,
           #Above you mention how thick the scalebar should be
           dist = 50, dist_unit = "km",
           transform = T, model = "WGS84",
           location = "topright",
           st.size = 4,
           #Above you have the font size of the numbers below the scalebar
           st.dist =0.2,
           #Above you have the distance between the bar and the text, as a proportion of the y axis.
           height=0.2)
map_river_graph<-reposition_legend(map_river_graph, 'bottom left')

ggsave(map_river_graph,file="./Dropbox/Legacies_Project/Paper/figures/Figure_A15a.jpg", 
       height=19.42, width=23.2, units = "cm", dpi=300)


######
#Map2#
######

krajna_brd$coincides<-NA
krajna_brd$coincides[krajna_brd$river==1]<-"Yes"
krajna_brd$coincides[krajna_brd$river==0]<-"No"

cols.fill = c("Border - River" = alpha("blue", 0.2), 
              "Border - No River" = alpha("brown", 0.2))

map_x<-ggplot() + 
  geom_sf(data = HRV_adm0, colour = "grey60", fill = NA)+
  geom_sf(data = final_sp_coord_yes, aes(fill = "Border - River"), 
          color = "grey20", size = 0.06)+
  geom_sf(data = final_sp_coord_no, aes(fill = "Border - No River"), 
          color = "grey20", size = 0.06)+
  geom_sf(data = krajna_brd, aes(colour = as.factor(coincides)), 
          size = 0.5, show.legend = T)+
  scale_fill_manual(name = "Municipalities \nClosest to", values = cols.fill) +
  scale_color_manual(name = "Coincides \nRiver?", values=c("brown", "blue"))+
  theme_bw()+
  labs(x = "Longitude", y="Latitude")+
  coord_sf(ylim=c(42.4,46.669853), xlim=c(13.556495, 19.447201))+
  theme(legend.position="left")+
  theme_bw()+
  labs(x = "Longitude", y="Latitude")+
  theme(axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title=element_text(size=14),
        plot.title = element_text(hjust = 0.5),
        legend.position = c(1, 0),
        legend.box.background = element_rect(fill='white'),
        legend.background = element_blank(),
        legend.text=element_text(size=12))+
  ggsn::scalebar(x.min = 18.5, x.max = 19.1,
                 #Above you mention how long the scalebar should be
                 y.min = 46.2, y.max = 46.5,
                 #Above you mention how thick the scalebar should be
                 dist = 50, dist_unit = "km",
                 transform = T, model = "WGS84",
                 location = "topright",
                 st.size = 4,
                 #Above you have the font size of the numbers below the scalebar
                 st.dist =0.2,
                 #Above you have the distance between the bar and the text, as a proportion of the y axis.
                 height=0.2)

map_x<-reposition_legend(map_x, 'bottom left')
ggsave(map_x, file="./Dropbox/Legacies_Project/Paper/figures/Figure_A15b.jpg",
       height=19.42, width=23.2, units = "cm", dpi=300)


